<?php

// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------


namespace app\common\model;


use app\common\lib\Status;
use think\Exception;
use think\facade\Log;
use think\Model;
use think\model\concern\SoftDelete;

/**
 * 有关时间的模型
 * Class TimeModel
 * @package app\common\model
 */
class TimeModel extends Model
{

    /**
     * 自动时间戳类型
     * @var string
     */
    protected $autoWriteTimestamp = true;

    /**
     * 添加时间
     * @var string
     */
    protected $createTime = 'create_time';

    /**
     * 更新时间
     * @var string
     */
    protected $updateTime = 'update_time';

    /**
     * 软删除
     */
    use SoftDelete;

    /**
     * @var bool
     */
    protected $deleteTime = false;

    /**
     * 公共查询 正常数据
     * @param array $where
     * @param string $field
     * @param string $orderBy
     * @return array
     * @throws Exception
     */
    public function getBaseByNormalList(array $where = [], string $field = "*", string $orderBy = "id desc"): array
    {
        try {
            $lists = $this
                ->field($field)
                ->where($where)
                ->where('status', Status::mysql_normal)
                ->order($orderBy)
                ->select()
                ->toArray();
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        return $lists;
    }

    /**
     * @param array $where
     * @param string $field
     * @param string $orderBy
     * @return TimeModel|array|mixed|Model|null
     * @throws Exception
     */
    public function getBaseByNormalListFind(array $where = [], string $field = "*", string $orderBy = "id desc")
    {
        try {
            $lists = $this
                ->field($field)
                ->where($where)
                ->where('status', Status::mysql_normal)
                ->order($orderBy)
                ->find();
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        return $lists;
    }

    /**
     * @param array $where
     * @param string $field
     * @param string $orderBy
     * @return TimeModel|array|mixed|Model|null
     * @throws Exception
     */
    public function getBaseByListFind(array $where = [], string $field = "*", string $orderBy = "id desc")
    {
        try {
            $lists = $this
                ->field($field)
                ->where($where)
                ->order($orderBy)
                ->find();
        } catch (\Exception $e) {
            Log::error($e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            throw new Exception("查询失败");
        }
        return $lists;
    }

    /**
     * @param array $where
     * @param int $limit
     * @param string $field
     * @param string $orderBy
     * @param array|string $with 关联方法名称
     * @return array
     * @throws Exception
     */
    public function getPaginateByNormalListInfo(array $where = [], int $limit = 10, string $field = "*", string $orderBy = "id desc", $with = null): array
    {
        try {
            $lists = $this
                ->field($field)
                ->where($where)
                ->with($with)
                ->order($orderBy)
                ->paginate($limit)
                ->toArray();
        } catch (\Exception $e) {
            Log::error("查询失败:" . $e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            throw new Exception($e->getMessage());
        }
        return $lists;
    }

    /**
     * @param int $id
     * @param string $field
     * @return TimeModel|array|mixed|Model|null
     * @throws Exception
     */
    public function getBaseById(int $id = 0, string $field = '*')
    {
        try {
            $lists = $this
                ->field($field)
                ->where('id', $id)
                ->find();
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        return $lists;
    }

    /**
     * @param array $where
     * @param string $field
     * @param string $orderBy
     * @return array
     * @throws Exception
     */
    public function getBaseByListAll(array $where = [], string $field = "*", string $orderBy = "id desc"): array
    {
        try {
            $lists = $this
                ->field($field)
                ->where($where)
                ->order($orderBy)
                ->select()
                ->toArray();
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        return $lists;
    }

    /**
     * 公共状态方法
     * @return string[]
     */
    public function getStatusList()
    {
        return ['0' => '禁用', '1' => '启用',];
    }

    /**
     * @param int $id
     * @param array $data
     * @return bool
     * @throws Exception
     */
    public function updateDataSave(int $id = 0, array $data = []): bool
    {
        try {
            $result = $this->where('id', $id)
                ->save($data);
        } catch (\Exception $e) {
            throw new Exception("操作失败".$e->getMessage());
        }
        if ($result) {
            return true;
        } else {
            return false;
        }
    }

}